package com.sdry.web.controller.zc.quality;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sdry.model.llm.User;
import com.sdry.model.zc.ZcLeakHuntingCriterionEntity;
import com.sdry.model.zc.ZcLeakHuntingEntity;
import com.sdry.model.zc.ZcLeakHuntingTemporaryEntity;
import com.sdry.model.zc.ZcNumberedEntity;
import com.sdry.model.zc.ZcProductTestEntity;
import com.sdry.model.zc.ZcQueryCriteria;
import com.sdry.model.zc.ZcTestEntity;
import com.sdry.service.zc.ZcCriterionService;
import com.sdry.service.zc.ZcLeakHuntingService;
import com.sdry.utils.ResponseUtil;

/**
 * 
 * @ClassName:    ZcLeakHuntingController   
 * @Description:  检漏作业
 * @Author:       zc   
 * @CreateDate:   2018年10月8日 下午3:40:09   
 * @Version:      v1.0
 */
@Controller
@RequestMapping("leakHunting")
public class ZcLeakHuntingController {

	@Resource
	private ZcLeakHuntingService zcLeakHuntingService;
	@Resource
	private ZcCriterionService zcCriterionService;
	/**
	 * 进入检漏页面
	 * @return
	 */
	@RequestMapping("enterLeakHuntingPage")
	public String enterLeakHuntingPage(Model model){
		ZcLeakHuntingCriterionEntity zcLeakHuntingCriterionEntity = zcCriterionService.selectLeakHuntingCriterion();
		model.addAttribute("zcLeakHuntingCriterionEntity", zcLeakHuntingCriterionEntity);
		return "/zc/quality/leakHunting";
	}
	/**
	 * 录入检漏记录
	 * @param request
	 * @param response
	 * @param zcLeakHuntingEntity
	 */
	@RequestMapping("insertLeakHunting")
	public void insertLeakHunting(HttpServletRequest request,HttpServletResponse response,ZcLeakHuntingEntity zcLeakHuntingEntity,HttpSession httpSession){
		User user = (User) httpSession.getAttribute("currentUser");
		String userName = user.getUserName();
		zcLeakHuntingEntity.setTestPerson(userName);
		int affact = zcLeakHuntingService.insertLeakHunting(zcLeakHuntingEntity);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 将合格降级改变订单状态
	 * @param request
	 * @param response
	 * @param zcTestEntity
	 */
	@RequestMapping("updateProductStatus")
	public void updateProductStatus(HttpServletRequest request,HttpServletResponse response,ZcTestEntity zcTestEntity,HttpSession httpSession){
		int affact = zcLeakHuntingService.updateProductStatus(zcTestEntity);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 将异常处置推送到不合格处置
	 * @param request
	 * @param response
	 * @param zcTestEntity
	 */
	@RequestMapping("insertProductToTest")
	public void insertProductToTest(HttpServletRequest request,HttpServletResponse response,ZcTestEntity zcTestEntity,HttpSession httpSession){
		User user = (User) httpSession.getAttribute("currentUser");
		String userName = user.getUserName();
		zcTestEntity.settTestPerson(userName);
		int affact = zcLeakHuntingService.insertProductToTest(zcTestEntity);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 查看今日检漏记录
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 * @param httpSession
	 */
	@RequestMapping("selectTodayLogListCount")
	public void selectTodayLogListCount(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zcQueryCriteria,HttpSession httpSession){
		User user = (User) httpSession.getAttribute("currentUser");
		String userName = user.getUserName();
		Date date = new Date();
		zcQueryCriteria.setWorker(userName);
		zcQueryCriteria.setWorkTime(date);
		List<ZcLeakHuntingEntity> todayLogList = zcLeakHuntingService.selectTodayLogListCount(zcQueryCriteria);
		int count = zcLeakHuntingService.countTodayLogListCount(zcQueryCriteria);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(todayLogList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 查看今日检漏记录详情
	 * @param request
	 * @param response
	 * @param zcLeakHuntingEntity
	 * @param httpSession
	 */
	@RequestMapping("selectTodayLogList")
	public void selectTodayLogList(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zcQueryCriteria,HttpSession httpSession){
		User user = (User) httpSession.getAttribute("currentUser");
		String userName = user.getUserName();
		Date date = new Date();
		zcQueryCriteria.setWorker(userName);
		zcQueryCriteria.setWorkTime(date);
		List<ZcLeakHuntingEntity> todayLogList = zcLeakHuntingService.selectTodayLogList(zcQueryCriteria);
		int count = zcLeakHuntingService.countTodayLogList(zcQueryCriteria);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(todayLogList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过条码查询组件数点信息
	 * @param request
	 * @param response
	 * @param productCode
	 */
	@RequestMapping("selectNumberedInfoByCode")
	public void selectNumberedInfoByCode(HttpServletRequest request,HttpServletResponse response,String productCode){
		List<ZcNumberedEntity> zcNumberedEntity = zcLeakHuntingService.selectNumberedInfoByCode(productCode);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(zcNumberedEntity, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", 1);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过条码查询组件历史数点信息
	 * @param request
	 * @param response
	 * @param productCode
	 */
	@RequestMapping("selectNumberedInfoLogByCode")
	public void selectNumberedInfoLogByCode(HttpServletRequest request,HttpServletResponse response,String productCode){
		List<ZcNumberedEntity> zcNumberedEntity = zcLeakHuntingService.selectNumberedInfoLogByCode(productCode);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(zcNumberedEntity, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", 1);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过组件条码查询该组件N次检漏A、B面总数
	 * @param request
	 * @param response
	 * @param productCode
	 */
	@RequestMapping("selectLeakHuntingSumN")
	public void selectLeakHuntingSumN(HttpServletRequest request,HttpServletResponse response,String productCode){
		ZcLeakHuntingEntity selectLeakHuntingSumN = zcLeakHuntingService.selectLeakHuntingSumN(productCode);
		JSONObject json= JSONObject.fromObject(selectLeakHuntingSumN);  
		try {
			if(selectLeakHuntingSumN!=null){
				ResponseUtil.write(response, json);
			}else{
				ResponseUtil.write(response, 0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 保存检漏临时数据
	 * @param request
	 * @param response
	 * @param zcLeakHuntingTemporaryEntity
	 */
	@RequestMapping("saveLeakHunting")
	public void saveLeakHunting(HttpServletRequest request,HttpServletResponse response,ZcLeakHuntingTemporaryEntity zcLeakHuntingTemporaryEntity){
		int affact = zcLeakHuntingService.saveLeakHunting(zcLeakHuntingTemporaryEntity);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 查找堵漏临时表
	 * @param request
	 * @param response
	 * @param code
	 */
	@RequestMapping("selectLeakHuntingTabByCode")
	public void selectLeakHuntingTabByCode(HttpServletRequest request,HttpServletResponse response,String code){
		ZcLeakHuntingTemporaryEntity zcLeakHuntingTemporaryEntity = zcLeakHuntingService.selectLeakHuntingTabByCode(code);
		JSONObject json= JSONObject.fromObject(zcLeakHuntingTemporaryEntity);  
		try {
			if(zcLeakHuntingTemporaryEntity!=null){
				ResponseUtil.write(response, json);
			}else{
				ResponseUtil.write(response, 0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
